package a06_栈与队列;

import java.util.Stack;

/**
 * <p>
 * a06_逆波兰表达式求值复习5
 * </p>
 *
 * @author flyduck
 * @since 2025/5/19
 */
public class a06_逆波兰表达式求值复习5 {
    public int evalRPN(String[] tokens) {
        Stack<Integer> stack = new Stack<>();
        for (String token : tokens) {
            if(token.equals("+") || token.equals("-") || token.equals("*") || token.equals("/")){
                int num1 = stack.pop();
                int num2 = stack.pop();
                switch (token) {
                    case "+":
                        stack.push(num2 + num1);
                        break;
                    case "-":
                        stack.push(num2 - num1);
                        break;
                    case "*":
                        stack.push(num2 * num1);
                        break;
                    case "/":
                        stack.push(num2 / num1);
                        break;
                }
            }else {
                stack.push(Integer.valueOf(token));
            }
        }
        return stack.pop();
    }
}
